#############################################
#Summary statistics by district
#############################################

rm(list = ls())

#Functions
source("functions.R")

installPackageNotFound("data.table")

#############################################
#Read in Voter data
#############################################

#All voters
all.voters <- fread("voters.csv")

#Aggregate statistics by district
district_summary <- all.voters[,
list(`Number Assigned` = sum(Treatment),
`Mean Voting Propensity` = mean(VotePropensity),
`Mean Democratic Partisanship Score` = mean(Partisanship),
`Mean Percentage White` = mean(White)*100),
by = District]

#Add control turnout
control_turnout <- all.voters[Treatment == 0,
list(mean_turnout = mean(BinaryTurnout)*100),
by = District]

#Merge to summary
district_summary$`Mean Control Turnout` <- control_turnout[match(
  district_summary$District, District), mean_turnout]

#Convert to df
district_summary <- data.frame(district_summary)

#Format columns
district_summary[, c(3:6)] <- apply(district_summary[, c(3:6)], 2,
function(x)formatSig(x, 3))

#Sort by district
print(district_summary[order(district_summary$District), ])
